clear all;
path1      = spm_select(1,'dir','please data1 dir');
%����spm_select����ѡȡ���������ļ���
file1         = dir([path1,filesep, '*.mat']);


for i=1:length(file1) 
    load([path1,filesep,file1(i).name]);%�������
    %Z=Z.*dp2;%�Ƿ�����MASK
    %Z=triu(Z,1);
    %Z(isnan(Z)) = 0;%��NaNɾ��
    Z(Z==Inf)    = 0;%��������ɾ��
    Z(Z==-Inf)   = 0;%�Ѹ�����ɾ��
    or_all_default_se1(:,:,i)=Z;%
    clear Z  %��Z���     
end  
%�����һ�鱻�ԡ�
%==========================================================================
path2     = spm_select(1,'dir','please data2 dir');
%����spm_select����ѡȡ���������ļ���
file2         = dir([path2,filesep, '*.mat']);

for i=1:length(file2)
    load([path2,filesep,file2(i).name]);%�������
    %Z=Z.*consensus_feature;%�Ƿ�����MASK
    %Z=triu(Z,1);
    Z(isnan(Z)) = 0;%��NaNɾ��
    Z(Z==Inf)    = 0;%��������ɾ��
    Z(Z==-Inf)   = 0;%�Ѹ�����ɾ�� 
    or_all_default_se2(:,:,i)=Z;%
    clear Z  %��Z���     
end
%����ڶ��鱻�ԡ�
%==========================================================================
%������ȷ����Pֵ������
h=waitbar(0,'pls wait..');
%==========================================================================
N_iteration = 1000;%�û�����

%==========================================================================
sub_con=length(or_all_default_se1(:,1,1))+1;
%��һʱʹ�õ�
clear b;

    all_default_se1=or_all_default_se1;
    all_default_se2=or_all_default_se2;
    %�Ƚ���ά����������
    [m,n,p]=size(all_default_se1);
    all_default_se1=reshape(all_default_se1,m*n,p);
    all_default_se2=reshape(all_default_se2,m*n,p);
    %���ܾ���
    [a,b]=find(all_default_se1==0);
    all_default_se1(a,:)=[];
    all_default_se2(a,:)=[];
    %ɾ��Ϊ0����
%==========================================================================

rate = zeros(N_iteration, 2);      
no_sub = size(all_default_se1, 2);
%==========================================================================
for it = 1:N_iteration
  
    if it>1
        sub_order = randperm(no_sub);
    else
        sub_order = [1:no_sub];
    end
    
    waitbar(it/N_iteration,h,[num2str(it),'/',num2str(N_iteration)])
    %�ӵڶ��ο�ʼ�����û������Լ����½�����
 %=========================================================================
    all_se1 = all_default_se1;
    all_se2 = all_default_se2(:,sub_order);%�ӵڶ��ο�ʼ�����ڶ������ݽ������š�
    count1 = 0;%�ۼ�1-2��ƥ����ȷ����
    count2 = 0;%�ۼ�2-1��ƥ����ȷ����  
   
    for i=1: no_sub;  
        % using session 1 as database
        tt_corr = all_se2(:, i);
        tt_to_all = corr(tt_corr, all_se1);   %���ĸ�����0.4449
        [va, va_id] = max(tt_to_all);    %�ó���va��0.4449��va_id��Ӧ����ֵ     va_id��4
        if( i == va_id)        
            count1 = count1+1;
        end                
        clear tt_corr tt_to_all va va_id
        % using session 2 as database        
        tt_corr = all_default_se1(:, i);
        tt_to_all = corr(tt_corr, all_se2);
        [va, va_id] = max(tt_to_all);
        if( i == va_id)        
            count2 = count2+1;
        end
    end
    rate(it,:) =[count1/no_sub, count2/no_sub];
    %����������ȷ��
end
AA_output(1,1).accuracy_1=rate(1,1);
AA_output(1,1).p1=mean(rate(1,1)<=rate(:,1));
AA_output(1,2).accuracy_2=rate(1,2);
AA_output(1,2).p2=mean(rate(1,2)<=rate(:,2));
%���ڴ������Ľṹ��
close(h)
disp done
open AA_output%�����Ľ�����㿴��